﻿
CREATE FUNCTION [dbo].[TreeBuilderOrphansExist]()
RETURNS BIT
AS
BEGIN
if (select COUNT(*) from TreeNodeBase
left join
(select TreeNodeBaseID from TreeBuilderRoot
union
select TreeNodeBaseID from TreeNodeAdvancedQuery
union
select TreeNodeBaseID from TreeNodeAttributeNode
union
select TreeNodeBaseID from TreeNodeElementNode
union
select TreeNodeBaseID from TreeNodeFolder
union
select TreeNodeBaseID from TreeNodeImage
union
select TreeNodeBaseID from TreeNodeMenuItem
union
select TreeNodeBaseID from TreeNodeOperationNode
union
select TreeNodeBaseID from TreeNodePropertyBinding
union
select TreeNodeBaseID from TreeNodeQuery
union
select TreeNodeBaseID from TreeNodeRecycleBin
union
select TreeNodeBaseID from TreeNodeRootSource
union
select TreeNodeBaseID from TreeNodeTransferMapping) as TreeNodes
on TreeNodeBase.TreeNodeBaseID = TreeNodes.TreeNodeBaseID
where TreeNodes.TreeNodeBaseID is null) > 0
begin
	return 1
end

return 0

END